package org.jeecg.modules.demo.Kingdee.ApPay;


import jcifs.CIFSContext;
import jcifs.config.PropertyConfiguration;
import jcifs.context.BaseContext;
import jcifs.smb.SmbFile;
import jcifs.smb.SmbFileOutputStream;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Workbook;
import org.jeecgframework.poi.excel.ExcelExportUtil;
import org.jeecgframework.poi.excel.entity.TemplateExportParams;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.springframework.stereotype.Component;

import java.io.*;
import java.util.*;
import jcifs.smb.NtlmPasswordAuthentication;
/**
 * 自动导入银行对账单
 *
 * @Author Scott
 */
@Slf4j
@Component
public class TestKingdeeJob implements Job {
    @SneakyThrows
    @Override
    public void execute(JobExecutionContext jobExecutionContext) {
//        // 1. 配置参数
//        String localFilePath = "D:\\KDTemplate\\银行交易明细20251030103018.xlsx";
//        String remoteHost = "192.168.1.218";
//        String remoteShareName = "KDTemplate"; // 远程共享名（必须与命令行一致）
//        String remoteUserName = "WIN-P0HGKBONJH0\\administrator"; // 格式：主机名\用户名
//        String remotePassword = "cg@2022!DBkingdee";
//
//        try {
//            // 2. 初始化SMB上下文（关键：配置协议版本和加密）
//            Properties props = new Properties();
//            props.setProperty("jcifs.smb.client.dfs.disabled", "true"); // 禁用DFS
//            props.setProperty("jcifs.smb.client.minVersion", "SMB210"); // 最低支持SMB2.1
//            props.setProperty("jcifs.smb.client.maxVersion", "SMB311"); // 最高支持SMB3.11
//            props.setProperty("jcifs.encryptData", "true"); // 启用加密（与Windows匹配）
//            CIFSContext baseContext = new BaseContext(new PropertyConfiguration(props));
//
//            // 3. 传递凭据（分离用户名密码，避免编码问题）
//            String[] userParts = remoteUserName.split("\\\\"); // 拆分主机名和用户名
//            NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication(
//                    baseContext,
//                    userParts[0], // 主机名（domain）
//                    userParts[1], // 用户名
//                    remotePassword // 密码
//            );
//            CIFSContext context = baseContext.withCredentials(auth);
//
//            // 4. 构建远程路径（SMB格式：smb://主机IP/共享名/文件名）
//            String fileName = localFilePath.substring(localFilePath.lastIndexOf("\\") + 1);
//            String remoteSmbPath = String.format("smb://%s/%s/%s", remoteHost, remoteShareName, fileName);
//
//            // 5. 复制文件
//            try (InputStream in = new FileInputStream(localFilePath);
//                 OutputStream out = new SmbFileOutputStream(new SmbFile(remoteSmbPath, context))) {
//
//                byte[] buffer = new byte[1024 * 1024];
//                int len;
//                while ((len = in.read(buffer)) != -1) {
//                    out.write(buffer, 0, len);
//                }
//                log.info("文件复制成功！目标路径：{}", remoteSmbPath);
//            }
//
//        } catch (Exception e) {
//            log.error("文件复制失败", e);
//        }
        TemplateExportParams params = new TemplateExportParams(
                "WEB-INF/doc/专项支出用款申请书_map.xls");
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("date", "2014-12-25");
        map.put("money", 2000000.00);
        map.put("upperMoney", "贰佰万");
        map.put("company", "执笔潜行科技有限公司");
        map.put("bureau", "财政局");
        map.put("person", "JueYue");
        map.put("phone", "1879740****");
        List<Map<String, String>> listMap = new ArrayList<Map<String, String>>();
        for (int i = 0; i < 4; i++) {
            Map<String, String> lm = new HashMap<String, String>();
            lm.put("id", i + 1 + "");
            lm.put("zijin", i * 10000 + "");
            lm.put("bianma", "A001");
            lm.put("mingcheng", "设计");
            lm.put("xiangmumingcheng", "EasyPoi " + i + "期");
            lm.put("quancheng", "开源项目");
            lm.put("sqje", i * 10000 + "");
            lm.put("hdje", i * 10000 + "");

            listMap.add(lm);
        }
        map.put("maplist", listMap);

        Workbook workbook = ExcelExportUtil.exportExcel(params, map);
        File savefile = new File("D:/excel/");
        if (!savefile.exists()) {
            savefile.mkdirs();
        }
        FileOutputStream fos = new FileOutputStream("D:/excel/专项支出用款申请书_map.xls");
        workbook.write(fos);
        fos.close();
    }
}
